package com.platform.destiny.config;

import com.platform.destiny.dto.Result;
import com.platform.destiny.exception.RetCode;
import com.platform.destiny.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 授权异常处理器
 */
@Slf4j
@Component
public class AppDeniedHandler implements AccessDeniedHandler {


    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response, AccessDeniedException ex)
            throws IOException, ServletException {

        // 返回权限不足提示
        Result<String> result = Result.failure(RetCode.DENIED);
        ResponseUtils.response(response, result);


    }
}
